package org.ksd.pug.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.ksd.pug.config.resultex.err.PugValidationException;
import org.ksd.pug.config.resultex.result.ResultCodeEnum;
import org.ksd.pug.controller.BaseController;
import org.ksd.pug.pojo.User;
import org.ksd.pug.service.user.IUserService;
import org.ksd.pug.utils.fn.asserts.Vsserts;
import org.ksd.pug.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * UserController
 * 创建人:星辰同学<br/>
 * 时间：2022-02-21 01:56:54 <br/>
 * 源码下载：www.gitee.com
 * 飞哥B站地址：www.baidu.com
 * @version 1.0.0<br/>
 *
*/
@Controller
@Slf4j
public class UserController extends BaseController{

    @Autowired
    private IUserService userService;



   /**
     * 查询分类列表信息
     ** @path : /admin/user/load
     * @method: findUserList
     * @result : List<User>
     * 创建人:星辰同学
     * 创建时间：2022-02-21 01:56:54
     * @version 1.0.0
     * @return
     */
    @GetMapping("/user/load")
    @ResponseBody
    public List<User> findUserList() {
        return userService.findUserList();
    }

	/**
	 * 查询分类列表信息并分页
	 * @path : /admin/user/list
     * @method: findUsers
     * @param : userVo
     * @result : IPage<User>
	 * 创建人:星辰同学
	 * 创建时间：2022-02-21 01:56:54
	 * @version 1.0.0
	*/
    @GetMapping("/user/list")
    @ResponseBody
    public IPage<User> findUsers(@RequestBody UserVo userVo) {
        return userService.findUserPage(userVo);
    }


    /**
     * 根据分类id查询明细信息
     * @method: get/{id}
     * @path : /admin/user/get/{id}
     * @param : id
     * @result : User
     * 创建人:星辰同学
     * 创建时间：2022-02-21 01:56:54
     * @version 1.0.0
    */
    @GetMapping("/user/get/{id}")
    @ResponseBody
    public User getUserById(@PathVariable("id") String id) {
        if(Vsserts.isEmpty(id)){
           throw new PugValidationException(ResultCodeEnum.EMPTY_ERROR);
        }
        return userService.getUserById(new Long(id));
    }


	/**
	 * 保存和修改分类
     * @method: saveupdate
	 * @path : /admin/user/save
     * @param : user
     * @result : User
	 * 创建人:星辰同学
	 * 创建时间：2022-02-21 01:56:54
	 * @version 1.0.0
	*/
    @PostMapping("/user/saveupdate")
    @ResponseBody
    public User saveupdateUser(@RequestBody User user) {
        return userService.saveupdateUser(user);
    }


    /**
	 * 根据分类id删除分类
     * @method: delete/{id}
     * @path : /admin/user/delete/{id}
     * @param : id
     * @result : int
	 * 创建人:星辰同学
	 * 创建时间：2022-02-21 01:56:54
	 * @version 1.0.0
	*/
    @PostMapping("/user/delete/{id}")
    @ResponseBody
    public int deleteUserById(@PathVariable("id") String id) {
        if(Vsserts.isEmpty(id)){
            throw new PugValidationException(ResultCodeEnum.EMPTY_ERROR);
        }
        return userService.deleteUserById(new Long(id));
    }


   /**
   	 * 根据分类ids批量删除分类
     * @method: user/delBatch
     * @path : /admin/user/delBatch
     * @param : userVo
     * @result : boolean
   	 * 创建人:星辰同学
   	 * 创建时间：2022-02-21 01:56:54
   	 * @version 1.0.0
   	*/
    @PostMapping("/user/delBatch")
    public boolean delUser(@RequestBody UserVo userVo) {
        log.info("你要批量删除的IDS是:{}", userVo.getBatchIds());
        if (Vsserts.isEmpty(userVo.getBatchIds())) {
            throw new PugValidationException(ResultCodeEnum.EMPTY_ERROR);
        }
        return userService.delBatchUser(userVo.getBatchIds());
    }

}